package com.whcoding.base.project.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * <p>
 * Swagger2 配置
 * </p>
 *
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {

    @Bean
    public Docket createRestApi() {

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                .enable(false) 是否启用swagger
                .select()
                //RequestHandlerSelectors 配置扫描接口的方式
                //1 basePackage 指定要扫描的包
                //withMethodAnnotation 通过方法的注解扫描

                //any 扫描全部
                //none 不扫描
                .apis(RequestHandlerSelectors.any())
//               apis(RequestHandlerSelectors.basePackage("com.whcoding.swagger.controller")).
                //withClassAnnotation 扫描类上的注解
//                .apis(RequestHandlerSelectors.withClassAnnotation( RestController.class))
//                //withMethodAnnotation 扫描注解
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))

                //过滤掉某路径
                //不显示错误的接口地址
                .paths(Predicates.not(PathSelectors.regex("/error.*")))//错误路径不监控
                .build();
    }

    private ApiInfo apiInfo() {
        //作者信息
        Contact contact = new Contact("知否派",
                "","");

        return new ApiInfoBuilder()
                .title("spring-boot-demo")
                .description("这是一个简单的 Swagger API 演示")
                .contact(new Contact("知否派", "https://snailthink.github.io/myblogs/", "3188655855@qq.com")).
                version("1.0.0-SNAPSHOT")
                .build();
    }




    /**
     * 判断当前项目是哪个环境
     * @param environment
     * @return
     */
    private Boolean getProjectEnvironment(Environment environment) {
        //设置要显示的swagger环境
        Profiles profiles = Profiles.of("dev");
        //通过environment  acceptsProfiles 判断是否在自己设定的环境中
        boolean flag = environment.acceptsProfiles(profiles);
        System.out.println(flag);
        return flag;
    }

}
